//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


using System;
using System.Collections.Generic;
using Shp.Runtime.Sitka.ServiceAgent;
using System.Globalization;
using Shp.Runtime.Sitka.ServiceAgent.Entities;
using Shp.Runtime.Sitka.ServiceAgent.EntityMappers;
using Shp.Runtime.Common;
using System.Linq;
using Shp.Runtime.DataModel.Sitka.Constants;

namespace Shp.Runtime.DataModel.Sitka
{
    public class SystemDataModel
    {        
        private IRepositoryFactory repositoryFactory;

        public SystemDataModel(IRepositoryFactory factory)
        {
            repositoryFactory = factory;
        }

        public SystemDataModel()
        {
            repositoryFactory = new GenericRepositoryFactory();
        }

        public Guid GetTenantId(string tenantAlias)
        {
            Guid tenantId = Guid.Empty;            
            tenantAlias = tenantAlias.ToLowerInvariant();
            using (Repository<GenericExtensibleEntity> repository = repositoryFactory.MetadataRepository)
            {
                IEnumerable<GenericExtensibleEntity> queryResult = repository.Search(String.Format(CultureInfo.InvariantCulture, Constants.SitkaMetadataQueries.GetTenantByAlias, tenantAlias));

                if (queryResult.Count((entity => entity.GetFieldValue(EntityFields.Alias) == tenantAlias)) > 0)
                {
                    GenericExtensibleEntity tenant = queryResult.First(entity => entity.GetFieldValue(EntityFields.Alias) == tenantAlias);
                    tenantId = tenant.Id;
                }
            }
            return tenantId;
        }

        public string GetTenantAlias(Guid tenantId)
        {
            string alias = null;
            using (Repository<GenericExtensibleEntity> repository = repositoryFactory.MetadataRepository)
            {
                GenericExtensibleEntity tenant = repository.GetById(tenantId);
                if (tenant != null)
                {
                    alias = tenant.GetFieldValue(EntityFields.Alias);
                }
            }
            return alias;
        }

        public Guid[] GetAllTenantIds()
        {
            List<Guid> ids = new List<Guid>();
            using (Repository<GenericExtensibleEntity> repository = repositoryFactory.MetadataRepository)
            {
                IEnumerable<GenericExtensibleEntity> tenants = repository.Search(SitkaMetadataQueries.GetTenants);
                foreach (GenericExtensibleEntity tenant in tenants)
                {
                    ids.Add(tenant.Id);
                }
            }
            return ids.ToArray();
        }
    }
}
